// pages/assemble/assemble.js
var common = require("../../common/common.js").common;

var leader = require("../../common/leader.js").leader;

const app = getApp();

Page({
    /**
   * 页面的初始数据
   */
    data: {
        selectClass: 1,
        //选择分类
        page: 1,
        //分页页码
        goods: [],
        //商品
        class: [],
        //分类
        myleaderinfo: []
    },
    /**
   * 生命周期函数--监听页面加载
   */
    onLoad: function(options) {
        this.setData({
            domain: app.globalData.domain.slice(0, -10)
        });
        wx.hideShareMenu();
        let that = this;
        leader.getMyLeaderInfo();
        //公用方法团长请求
                that.getClass();
        that.getBase();
    },
    /**
   * 生命周期函数--监听页面初次渲染完成
   */
    onReady: function() {},
    /**
   * 生命周期函数--监听页面显示
   */
    onShow: function() {},
    /**
   * 生命周期函数--监听页面隐藏
   */
    onHide: function() {},
    /**
   * 生命周期函数--监听页面卸载
   */
    onUnload: function() {},
    /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
    onPullDownRefresh: function() {},
    /**
   * 页面上拉触底事件的处理函数
   */
    onReachBottom: function() {
        this.getGoods();
    },
    /**
   * 用户点击右上角分享
   */
    onShareAppMessage: function() {},
    //全局基础配置信息
    getBase: function() {
        var that = this;
        common.request({
            endpoint: "set/app_base",
            method: "get",
            data: {},
            success: res => {
                wx.setNavigationBarTitle({
                    title: res.extra.title
                });
                wx.setNavigationBarColor({
                    frontColor: "#ffffff",
                    backgroundColor: res.extra.color_main
                });
                that.setData({
                    color_main: res.extra.color_main,
                    color_minor: res.extra.color_minor,
                    apptitle: res.extra.title,
                    style: res.extra.style,
                    leader_change: res.extra.l_change,
                    leader_express: res.extra.l_express,
                    rp_name: res.extra.rp_name,
                    order_start: res.extra.order_start,
                    order_start_switch: res.extra.order_start_switch,
                    leaderName: res.extra.leader_title ? res.extra.leader_title : "团长",
                    communityName: res.extra.community_title ? res.extra.community_title : "社区",
                    leader_address_on: res.extra.leader_address_on
                });
                //处理模块开关
                                var features = common.getModulesSwich(res.extra.features);
                //处理数据格式
                                that.setData({
                    features: features
                });
                //设置模块颜色
                                var componentStyle = that.data.componentStyle;
                that.setData({
                    componentStyle: {
                        ...componentStyle,
                        color_main: !res.extra.color_main ? "#FE513B" : res.extra.color_main,
                        color_minor: !res.extra.color_minor ? "#FE513B" : res.extra.color_minor
                    }
                });
            }
        });
    },
    //获取分类
    getClass: function() {
        let that = this;
        common.request({
            endpoint: "Groupbuy/catalog/list",
            method: "post",
            header: {},
            success: function(res) {
                that.setData({
                    selectClass: res.extra[0].id,
                    class: res.extra
                });
                that.getGoods();
            }
        });
    },
    //选择分类
    selectClass: function(e) {
        let that = this;
        let selectClass = e.currentTarget.dataset.id;
        that.setData({
            selectClass: selectClass,
            page: 1,
            goods: []
        });
        that.getGoods();
    },
    //获取商品
    getGoods: function() {
        let that = this;
        let page = that.data.page;
        let selectClass = that.data.selectClass;
        common.request({
            endpoint: "Groupbuy/product/list",
            method: "post",
            data: {
                leaderId: that.data.myleaderinfo.leader_id,
                catelog_id: selectClass,
                page: page,
                rows: 10
            },
            header: {},
            success: function(res) {
                var data = that.data.goods;
                let goods = page == 1 ? res.extra : data.concat(res.extra);
                if (res.extra.length > 0) {
                    that.setData({
                        goods: goods
                    });
                    that.data.page = page + 1;
                }
            }
        });
    }
});